package com.kmxd.ams.controller.archive;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.archive.IArchiveTypeSvc;
import com.kmxd.ams.client.archive.dto.ArchiveTypeAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveTypeEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveTypeOpenDTO;
import com.kmxd.ams.client.archive.dto.ArchiveTypeQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveFondsVO;
import com.kmxd.ams.client.archive.vo.ArchiveTypeListVO;
import com.kmxd.ams.client.archive.vo.ArchiveTypeVO;
import com.kmxd.ams.client.archive.vo.FondsOrArchiveTypeVO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.archive.mapper.ArchiveTypeMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 门类|档案库配置主表 前端控制器
 *
 * @author ZuoLG
 * @since 2022-07-15
 */
@RestController
@Tag(name = "门类|档案库配置主表")
@RequestMapping("/archive/archiveType")
@AllArgsConstructor
public class ArchiveTypeCtl {

  private IArchiveTypeSvc archiveTypeService;
  private final ArchiveTypeMapper archiveTypeMapper;

  /** 分页查询 */
  @Operation(summary = "分页查询")
  @GetMapping("/selectPage")
  @SaCheckLogin
  @AutoLog(
      module = "门类|档案库配置主表",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "分页查询")
  public R<Page<ArchiveTypeListVO>> selectPage(Page reqPage, ArchiveTypeQueryDTO req) {
    req.setFondsId(
        ObjectUtil.isNotEmpty(req.getFondsId()) ? req.getFondsId() : SecurityUtil.getFondsId());
    return R.success(archiveTypeService.selectPage(reqPage, req));
  }

  /** 档案库列表 */
  @Operation(summary = "档案库列表")
  @GetMapping("/list")
  @SaCheckLogin
  public R<List<ArchiveTypeListVO>> selectList(ArchiveTypeQueryDTO req) {
    req.setFondsId(
        ObjectUtil.isNotEmpty(req.getFondsId()) ? req.getFondsId() : SecurityUtil.getFondsId());
    req.setComId(ObjectUtil.isNotEmpty(req.getComId()) ? req.getComId() : SecurityUtil.getCropId());
    req.setRoleId(
        ObjectUtil.isNotEmpty(req.getRoleId())
            ? req.getRoleId()
            : SecurityUtil.getLoginUser().getRoleId());
    return R.success(archiveTypeService.selectList(req));
  }

  /** 查询ams门类 */
  @Operation(summary = "查询ams门类")
  @GetMapping("/listByAms")
  @SaCheckLogin
  public R<List<ArchiveTypeListVO>> listByAms(ArchiveTypeQueryDTO req) {
    return R.success(archiveTypeService.selectListByAms());
  }

  /** 当前单位下所有全宗及档案库 */
  @Operation(summary = "当前单位下所有全宗及档案库")
  @GetMapping("/archTypeByCrop")
  @SaCheckLogin
  public R<List<ArchiveFondsVO>> archTypeByCrop(@RequestParam Long roleId) {
    return R.success(archiveTypeService.archTypeByCrop(roleId));
  }

  /** 新增 */
  @Operation(summary = "新增")
  @PostMapping("/add")
  @SaCheckPermission("archive:archiveType:add")
  @AutoLog(module = "门类|档案库配置主表", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody ArchiveTypeAddDTO req) {
    req.setFondsId(
        ObjectUtil.isEmpty(req.getFondsId()) ? SecurityUtil.getFondsId() : req.getFondsId());
    archiveTypeService.add(req);
    return R.success();
  }

  /** 修改 */
  @Operation(summary = "修改")
  @PostMapping("/edit")
  @SaCheckPermission("archive:archiveType:edit")
  @AutoLog(
      module = "门类|档案库配置主表",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.HIGH,
      title = "修改")
  public R<Void> edit(@Validated @RequestBody ArchiveTypeEditDTO req) {
    archiveTypeService.edit(req);
    return R.success();
  }

  /** 开放门类 */
  @Operation(summary = "开放门类")
  @PostMapping("/openArchiveType")
  @SaCheckPermission("archive:archiveType:edit")
  @AutoLog(
      module = "门类|档案库配置主表",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.HIGH,
      title = "开放门类")
  public R<Void> openArchiveType(@Validated @RequestBody ArchiveTypeOpenDTO req) {
    archiveTypeService.openArchiveType(req);
    return R.success();
  }

  /** 当前单位下全宗及档案库 */
  @Operation(summary = "当前单位下全宗及档案库")
  @PostMapping("/getFondsOrArchiveTypeByCrop")
  @AutoLog(
      module = "门类|档案库配置主表",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.MEDIUM,
      title = "当前单位下全宗及档案库")
  public R<FondsOrArchiveTypeVO> getFondsOrArchiveTypeByCrop(
      @Validated @RequestBody ArchiveTypeEditDTO req) {
    req.setFondsId(
        ObjectUtil.isNotEmpty(req.getFondsId()) ? req.getFondsId() : SecurityUtil.getFondsId());
    return R.success(archiveTypeService.getFondsOrArchiveTypeByCrop());
  }

  /** 删除 */
  @Operation(summary = "删除")
  @PostMapping("/remove")
  @SaCheckPermission("archive:archiveType:remove")
  @AutoLog(
      module = "门类|档案库配置主表",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.HIGH,
      title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    archiveTypeService.remove(ids);
    return R.success();
  }

  /** 查看 */
  @Operation(summary = "查看")
  @GetMapping("/view")
  @AutoLog(module = "门类|档案库配置主表", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查看")
  public R<ArchiveTypeVO> view(@RequestParam Long id) {
    return R.success(archiveTypeService.view(id));
  }

  /** 导出 */
  @Operation(summary = "导出")
  @GetMapping("/export")
  @AutoLog(
      module = "门类|档案库配置主表",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.HIGH,
      title = "导出")
  @SneakyThrows
  public void export(ArchiveTypeQueryDTO req, HttpServletResponse response) {
    String filename = "门类|档案库配置主表列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    req.setFondsId(
        ObjectUtil.isNotEmpty(req.getFondsId()) ? req.getFondsId() : SecurityUtil.getFondsId());
    req.setComId(ObjectUtil.isNotEmpty(req.getComId()) ? req.getComId() : SecurityUtil.getCropId());
    req.setRoleId(
        ObjectUtil.isNotEmpty(req.getRoleId())
            ? req.getRoleId()
            : SecurityUtil.getLoginUser().getRoleId());
    EasyExcel.write(response.getOutputStream())
        .head(ArchiveTypeListVO.class)
        .sheet("门类|档案库配置主表列表")
        .doWrite(archiveTypeService.selectList(req));
  }
}
